package ssm.dao.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import ssm.dao.OrderDao;
import ssm.pojo.Orderitem;
import ssm.pojo.Orders;
import ssm.pojo.User;
import util.Dbutil;

import java.sql.SQLException;
import java.util.List;


public class OrderDaoImpl implements OrderDao {

    /**
     * 添加订单
     *
     * @param order 订单实例
     * @return 影响的行数
     * @throws SQLException
     */

    public int addOrder(Orders order) throws SQLException {
//        QueryRunner qr = new QueryRunner(Dbutil.getDataSource());
        QueryRunner qr = new QueryRunner();
        String sql = "insert into orders values (?,?,?,?,?,?,?,?)";
        int update = qr.update(Dbutil.getConnection(), sql,
                order.getOid(),
                order.getOrdertime(),
                order.getTotal(),
                order.getState(),
                order.getAddress(),
                order.getName(),
                order.getTelephone(),
                order.getUser().getUid()
        );
        return update;
    }

    /**
     * 添加订单项
     *
     * @param orderitem 订单项
     * @return 影响行数
     * @throws SQLException
     */
    public int addOrderitem(Orderitem orderitem) throws SQLException {
//        QueryRunner qr = new QueryRunner(Dbutil.getDataSource());
        QueryRunner qr = new QueryRunner();
        String sql = "insert into orderitem values (?,?,?,?,?)";
        int update = qr.update(Dbutil.getConnection(), sql,
                orderitem.getItemid(),
                orderitem.getCount(),
                orderitem.getSubtotal(),
                orderitem.getProduct().getPid(),
                orderitem.getOrders().getOid()
        );
        return update;
    }


    /**
     * 通过用户查询分页订单
     * @param user 用户
     * @param number 当前页数
     * @param total 每页显示条数
     * @return 订单
     * @throws SQLException
     */
    public List<Orders> findOrderBU(User user,int number,int total) throws SQLException {
        QueryRunner qr = new QueryRunner(Dbutil.getDataSource());
        String sql = "select * from orders where uid=? limit ?,?";
        List<Orders> orders = qr.query(sql, new BeanListHandler<Orders>(Orders.class),
                user.getUid(),
                (number-1)*total,
                total);
        return orders;
    }

    /**
     *
     * 通过订单查询订单项
     * @param orders 订单
     * @return 订单项
     * @throws SQLException
     */
    public  List<Orderitem> fintOrderItemBO(Orders orders) throws SQLException {
        QueryRunner qr = new QueryRunner(Dbutil.getDataSource());
        String sql = "select * from orderitem where oid=?";
        List<Orderitem> orderitem = qr.query(sql, new BeanListHandler<Orderitem>(Orderitem.class),
                orders.getOid());
        return orderitem;
    }

    /**
     * 查询该用户的总订单数
     * @param user 用户
     * @return 总订单数
     * @throws SQLException
     */
    public int findCountOrder(User user) throws SQLException {
        QueryRunner qr = new QueryRunner(Dbutil.getDataSource());
        String sql = "select count(oid) from orders where uid = ?";
        Object query = qr.query(sql, new ScalarHandler<>(),user.getUid());
        int i = Integer.parseInt(String.valueOf(query));
        return i;
    }
}
